<?php
/**
 * Date: 6/13/12
 * Time: 2:41 PM
 */

require_once 'bootstrap.php';
include_once 'includes/formvalidator.php';
include_once 'includes/utils.php';

if (isset($_POST['Submit'])) {
	// Validation
	$validations = array(
		'firstName' => array('type' => 'words', 'message' => 'First name invalid'),
		'lastName' => array('type' => 'words', 'message' => 'Last name invalid'),
		'orgName' => array('type' => 'alfanum', 'message' => 'Name of league/org must invalid '),
		'address' => array('type' => 'alfanum', 'message' => 'Address invalid'),
		'city' => array('type' => 'alfanum', 'message' => 'You must enter a valid address'),
		'zipcode' => array('type' => 'anything', 'message' => 'invalid zipcode'),
		'phone' => array('type' => 'phone', 'message' => 'invalid phone number'),
		'email' => array('type' => 'email', 'message' => 'You must enter a valid email address'),
		'url' => array('type' => 'url', 'message' => 'You must enter a valid link'),
		'ageTo' => array('type' => 'number', 'message' => 'You must enter a valid age'),
		'ageFrom' => array('type' => 'number', 'message' => 'You must enter a valid age'),
		'registrationStart' => array('type' => 'date', 'message' => 'You must enter a valid registration start date'),
		'registrationEnd' => array('type' => 'date', 'message' => 'You must enter a valid registration end date'),
		'seasonStart' => array('type' => 'date', 'message' => 'You must enter a valid season start date'),
		'seasonEnd' => array('type' => 'date', 'message' => 'You must enter a valid season end date'),
	);
	// Required array
	$required = array('firstName', 'lastName', 'orgName', 'address', 'city', 'zipcode', 'phone', 'email',
		'url', 'sport', 'ageTo', 'ageFrom', 'gender', 'eligibleZipcodes', 'registrationStart',
		'registrationEnd', 'seasonStart', 'seasonEnd');
	$sanatize = array();
	foreach ($validations as $item_name => $options) {
		switch ($options['type']) {
			case 'anything':
				array_push($sanatize, $item_name);
				break;
			case 'number':
				$sanatize[$item_name] = 'int';
				break;
			case 'price':
				$sanatize[$item_name] = 'float';
				break;
			case 'email':
				$sanatize[$item_name] = 'email';
				break;
			default:
				break;
		}
	}
	$validator = new FormValidator($validations, $required, $sanatize);

	if ($validator->validate($_POST)) {
		// Very important!!
		$_POST = $validator->sanatize($_POST);
		$data = array($_POST['firstName'], $_POST['lastName'], $_POST['orgName'], $_POST['address'], $_POST['city'], $_POST['zipcode'], $_POST['phone'], $_POST['email'], $_POST['url'], $_POST['sport'], $_POST['ageTo'], $_POST['ageFrom'], $_POST['gender'], $_POST['eligibleZipcodes'], date('Y-m-d H:i:s',strtotime($_POST['registrationStart'])), date('Y-m-d H:i:s',strtotime($_POST['registrationEnd'])), date('Y-m-d H:i:s',strtotime($_POST['seasonStart'])), date('Y-m-d H:i:s',strtotime($_POST['seasonEnd'])));
		$return_code = $my_db->execute('INSERT leagues (`firstName`, `lastName`, `orgName`, `address`, `city`, `zipcode`, `phone`, `email`, `url`, `sport`, `ageTo`, `ageFrom`, `gender`, `eligibleZipcodes`, `registrationStart`, `registrationEnd`, `seasonStart`, `seasonEnd`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)', $data);
		$_POST = array();

		if ($return_code > 0) {
			$mess = getADMSuccess("League have been updated.");
			$url_to_email = $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/add_edit_league.php?id=' . $return_code;
			sendSuccessEmail('zeeshanhashmi@gmail.com', 'zeeshanhashmi@gmail.com', 'zeeshanhashmi@gmail.com', 'LOREM IPSUM', $_SERVER['REMOTE_ADDR']);
		}

	} else {
		$form_errors = $validator->getError();

		if (!empty($form_errors)) {
			foreach ($form_errors as $error) {
				$errors .= li($error);
			}
			$mess = getADMError($errors);
		}
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
	<title><?php echo $front_title; ?></title>
	<?php include("__head_tag.php");?>
	<SCRIPT language=Javascript>

	</SCRIPT>
</head>
<body>
<div id="wrap">
<?php include("__header.php"); ?>

<?php include("__nav.php"); ?>

<div id="content">
<p>&nbsp;</p>

<h2 class="header">Post your league </h2>
	<?php echo $mess;?>
<p>

<form action="" name="f1" id="f1" method="post" class="theForm" onsubmit="return validate();">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
	<td width="38%" class="theField">First Name :</td>
	<td width="62%">
		<input name="firstName" value="<?php echo _getEcho($_POST,'firstName'); ?>" type="text" class="theInput require" id="firstName" size="30" maxlength="50"/>
	</td>
</tr>
<tr>
	<td class="theField"> Last Name :</td>
	<td>
		<input name="lastName" value="<?php echo _getEcho($_POST,'lastName'); ?>" type="text" class="theInput require" id="lastName" size="30" maxlength="50"/></td>
</tr>
<tr>
	<td class="theField">Name of league / org :</td>
	<td><input name="orgName" value="<?php echo _getEcho($_POST,'orgName'); ?>" type="text" class="theInput require" id="orgName" size="30" maxlength="50"/></td>
</tr>
<tr>
	<td class="theField"> Address :</td>
	<td><input name="address" value="<?php echo _getEcho($_POST,'address'); ?>" type="text" class="theInput require" id="address" size="30" maxlength="255"/></td>
</tr>
<tr>
	<td class="theField">City :</td>
	<td><input name="city" value="<?php _getEcho($_POST,'city'); ?>" type="text" class="theInput require" id="city" size="30" maxlength="150"/></td>
</tr>
<tr>
	<td class="theField">State :</td>
	<td>
		<select name="state" class="theInput require" id="state">
			<option value="">Choose one....</option>
			<?php
			$states = getStateName();
			foreach ($states as $key => $val):
				?>
				<option value="<?=$key;?>" <?php if (isset($_POST['state']) && $_POST['state'] == $key) echo "SELECTED" ?> ><?=$val;?></option>
				<?php endforeach;?>
		</select>
	</td>

</tr>
<tr>
	<td class="theField"> Zipcode:</td>
	<td><input name="zipcode" value="<?php _getEcho($_POST,'zipcode'); ?>" type="text" class="theInput require" id="zipcode" size="30" maxlength="10"
	           onkeypress="return isNumberKey(event)"/></td>
</tr>
<tr>
	<td class="theField"> Phone:</td>
	<td><input name="phone" value="<?php echo _getEcho($_POST,'phone'); ?>" type="text" class="theInput require" id="phone" size="30" maxlength="15"/></td>
</tr>
<tr>
	<td class="theField"> Email:</td>
	<td><input name="email" value="<?php echo _getEcho($_POST,'email'); ?>" type="text" class="theInput require" id="email" size="30" maxlength="50"/></td>
</tr>
<tr>
	<td class="theField"> Url:</td>
	<td><input name="url" value="<?php echo _getEcho($_POST,'url'); ?>" type="text" class="theInput require" id="url" size="30" maxlength="255"/></td>
</tr>
<tr>
	<td class="theField"> Sport:</td>
	<td>
		<select name="sport" class="theInput require" id="sport">
			<option value="">Choose one....</option>
			<?php
				$sports = $my_db->select_pairs('SELECT * FROM sports');
				foreach ($sports as $id => $name):
				?>
				<option value="<?=$id?>" <?if (isset($_POST['gender']) && $_POST['sport']== $id) echo "SELECTED"?> ><?=$name?></option>
				<?php endforeach; ?>
		</select>
	</td>
</tr>
<tr>
	<td class="theField"> Age From:</td>
	<td><input name="ageFrom" value="<?php echo _getEcho($_POST,'ageFrom'); ?>" type="text" class="theInput require" id="ageFrom" size="30" maxlength="10"
	           onkeypress="return isNumberKey(event)"/></td>
</tr>
<tr>
	<td class="theField"> Age To:</td>
	<td><input name="ageTo" value="<?php echo _getEcho($_POST,'ageTo'); ?>" type="text" class="theInput require" id="ageTo" size="30" maxlength="10"
	           onkeypress="return isNumberKey(event)"/></td>
</tr>
<tr>
	<td class="theField"> Gender:</td>
	<td>
		<select id="gender" name="gender" class="theInput require">
			<option value="">Choose...</option>
			<option value="male" <?php if (isset($_POST['gender']) && $_POST['gender'] == 'male') echo "checked = 'checked'" ?> >Male</option>
			<option value="female" <?php if (isset($_POST['gender']) &&$_POST['gender'] == 'female') echo "checked = 'checked'"?> >Female</option>
			<option value="both" <?php if (isset($_POST['gender']) && $_POST['gender'] == 'both') echo "checked = 'checked'"?> >both</option>
		</select>
	</td>
</tr>
<tr>
	<td class="theField"> Eligible Zipcodes:</td>
	<td><input name="eligibleZipcodes" value="<?php echo _getEcho($_POST,'eligibleZipcodes'); ?>" type="text" class="theInput require" id="eligibleZipcodes" size="30"
	           maxlength="250"/></td>
</tr>
<tr>
	<td class="theField">Registration Start Date:</td>
	<td><input name="registrationStart" value="<?php echo _getEcho($_POST,'registrationStart'); ?>" type="text" class="theInput require" id="registrationStart" size="30"
	           maxlength="10"/></td>
</tr>
<tr>
	<td class="theField">Registration End Date:</td>
	<td><input name="registrationEnd" value="<?php echo _getEcho($_POST,'registrationEnd'); ?>" type="text" class="theInput require" id="registrationEnd" size="30"
	           maxlength="10"/></td>
</tr>
<tr>
	<td class="theField">Season Start Date:</td>
	<td><input name="seasonStart" value="<?php echo _getEcho($_POST,'seasonStart'); ?>" type="text" class="theInput require" id="seasonStart" size="30" maxlength="30"/>
	</td>
</tr>
<tr>
	<td class="theField">Season Start Date:</td>
	<td><input name="seasonEnd" value="<?php echo _getEcho($_POST,'seasonEnd'); ?>" type="text" class="theInput require" id="seasonEnd" size="30" maxlength="30"/></td>
</tr>
<tr>
	<td>
		<div align="right">
			<a href="#" onclick="
            document.getElementById('captcha').src='captcha.php?'+Math.random();
            document.getElementById('captcha-form').focus();"
			   id="change-image">Not readable? Change text.</a>
		</div>
	</td>
	<td><img src="captcha.php" id="captcha"/></td>
</tr>
<tr>
	<td class="theField">Image Verification :</td>
	<td><input name="captcha" type="text" class="theInput require" id="captcha" size="30" maxlength="100"/></td>
</tr>
<tr>
	<td colspan="2">
		<hr/>
	</td>
</tr>
<tr>
	<td colspan="2">
		<div align="center">
			<input name="Submit" type="submit" class="theButton" id="Submit" value="Submit"/>
		</div>
	</td>
</tr>
</table>

</form>
</p>
</div>
<?php include("__footer.php"); ?>
</div>
</body>
</html>
